package com.rw.coco.handler.message.reply.handler.command;

import com.rw.coco.handler.message.event.CocoMessageEvent;
import com.rw.coco.handler.message.reply.ReplyHandler;
import com.rw.coco.handler.message.reply.handler.command.assistant.CommandMatcher;
import com.rw.coco.handler.message.reply.handler.command.sender.RASender;
import com.rw.coco.util.config.CocoCommandConfig;
import com.rw.coco.util.config.CocoConfig;
import com.rw.coco.util.config.CocoOption;
import com.rw.coco.util.DiceRule;
import com.rw.tool.util.string.StringUtil;

/*
    1、多重检定         .ra2#斗殴
    2、房规/标准规则     .ra为房规，.rc为标准规则
    3、奖励/惩罚骰子     .rab2斗殴  两个奖励骰子（奖励:b   惩罚:p）最多9个骰子
    4、属性值修正        .ra斗殴+10 检定斗殴+10

    .ra2#b2斗殴80
    .ra2#p2斗殴+10
*/

/**
 * 能力检定
 * .ra/.rc
 */
public class RAHandler implements ReplyHandler {
    private static final int RA = 0;

    private final CommandMatcher matcher;

    public final boolean funcSwitch;

    public RAHandler(CocoConfig cocoConfig) {
        CocoCommandConfig cocoCommandConfig = cocoConfig.getCocoCommandConfig();
        CocoOption cocoOption = cocoConfig.getCocoOption();

        String rcCommand = cocoCommandConfig.commandProp.get(CocoCommandConfig.RC);
        String raCommand = cocoCommandConfig.commandProp.get(CocoCommandConfig.RA);
        this.matcher = CommandMatcher.of(raCommand, rcCommand);
        this.funcSwitch = cocoOption.optionProp.getBoolean(CocoOption.COCO_RA);
    }

    @Override
    public boolean trigger(CocoMessageEvent cocoMessageEvent) {
        String message = cocoMessageEvent.getMessage();
        return funcSwitch && matcher.isMatch(message);
    }

    @Override
    public void handler(CocoMessageEvent cocoMessageEvent) {
        // 分离消息与前缀
        String message = matcher.subTrim(cocoMessageEvent.getMessage()).replaceAll("[\\s\\n]", "");
        RASender raSender = new RASender(cocoMessageEvent);

        // 根据群房规获取骰点规则
        DiceRule diceRule = raSender.getDiceRule(matcher.isMatch(message, RA));

        // 处理消息
        String result = raSender.send(message, diceRule);

        // 发送消息
        if (!StringUtil.isBlank(result)) {
            cocoMessageEvent.sendBack(result);
        }
    }

    @Override
    public int priority() {
        return matcher.priority();
    }
}
